home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / nocz12.zip / NOCZ1.ASM < prev    next >
Assembly Source File  |  1989-10-13  |  5KB  |  193 lines

  1.     Name nocz
  2.     Title    Control Z stripping filter.
  3.     page    ,132
  4. Comment ~
  5.  
  6.     This program is a filter that removes all occurrances
  7.     of the ASCII character 26 (^Z) from its output stream.
  8.     It will optionally accept a filename from which to read
  9.     input.  All output goes to the     standard output device
  10.     unless redirected.
  11.  
  12.     Examples:
  13.         nocz abc.txt    read input from abc.txt,
  14.                 output goes to screen.
  15.         nocz abc.txt >abcnew.txt
  16.                 read input from abc.txt,
  17.                 output goes to abcnew.txt.
  18.         nocz <abc.txt >abcnew.txt
  19.                 same as previous example
  20. Toad Hall Tweak, Oct 89
  21.  - Bumping working buffer to BIG...
  22.  - Simplistic PSP command line parsing didn't properly handle
  23.    DOS redirection/filter usage.  Fixed.
  24.  - No reason at all to release memory .. unless maybe for filters??
  25.  
  26. ~
  27.  
  28. CR    equ    0DH
  29. LF    equ    0AH
  30. STDOUT    equ    1        ;Standard Output
  31.  
  32. ;===================================================================
  33.     CSEG    segment    public para 'CODE'
  34. ;===================================================================
  35. ;
  36. ;    command line is at 80h of psp - first byte is length
  37. ;
  38.     ORG    80h
  39. cmdline        label    byte        ;                v1.1
  40. ;
  41. ; .com starts at 100h - but must jump around any data area
  42. ;
  43.     ORG    100h            ; com file starts here
  44.     ASSUME    CS:CSEG,DS:CSEG,ES:CSEG
  45.  
  46. NoCz    proc    near            ;v1.1
  47.  
  48.     mov    AH,30h            ; get dos version
  49.     int    21h
  50.     cmp    AL,2            ; must be at least 2.0
  51.     jb    Oops
  52.  
  53.     call    Parse_CmdLine        ;parse cmdline for target filename v1.1
  54.                     ;(will give help and die if NO
  55.                     ;filename or redirection)
  56.  
  57.     xor    bp,bp            ;assume StdIn handle        v1.1
  58.     or    dx,dx            ;did we get a name?        v1.1
  59.     jz    Save_Handle        ;nope, use StdIn
  60.  
  61. ;v1.1    else DX -> filename start
  62.  
  63. ; now try to open the file
  64.  
  65.     mov    ax,3D00H        ;open file, read only        v1.1
  66.     int    21h            ; invoke function
  67.     jc    Oops            ; open error - quit
  68.  
  69.     mov    bp,ax            ;keep file handle in BP        v1.1
  70.  
  71. Save_Handle:
  72.     mov    DX,offset buffer    ;DX -> read/write buffer    v1.1
  73.  
  74. ;v1.1    We loop here with BIG buffers full of target file text
  75.  
  76. Again:
  77.     mov    BX,bp    ;handle        ; read from standard input or file v1.1
  78.     mov    CX,BUFSIZ        ; # of characters to read    v1.1
  79.     mov    AH,3Fh            ; dos read function
  80.     int    21h            ; invoke function
  81.     jc    Oops            ; error!  (error value in AL)    v1.1
  82.     or    ax,ax            ; if zero, end of file        v1.1
  83.     jz    Oops            ; (errorlevel 0)        v1.1
  84.  
  85.     mov    CX,AX            ; CX contains # characters read
  86.     mov    SI,dx    ;offset buffer    ; DS:SI is the "input" pointer    v1.1
  87.     mov    DI,SI            ; ES:DI is the "output" pointer
  88.     mov    ah,26            ;handy Ctrl Z constant        v1.1
  89.  
  90. Loop1:
  91.     lodsb                ; get a character from buffer
  92.     cmp    AL,ah    ;26        ; if ^Z then skip        v1.1
  93.     je    EatIt
  94.      stosb                ; put back into buffer
  95. EatIt:
  96.     loop    Loop1            ; and repeat for entire buffer
  97.  
  98.     mov    CX,DI            ; Now calculate the ouput character
  99.     sub    CX,dx    ;offset buffer    ; count
  100.     mov    BX,STDOUT        ; standard output device    v1.1
  101.     mov    AH,40h            ; dos write function
  102.     int    21h            ; invoke dos function
  103.  
  104.     jmp    short Again        ; repeat until end of file or error
  105.  
  106. Oops:
  107.     mov    ah,4CH            ;terminate (errorlevel in AL)    v1.1
  108.     int    21H            ;                v1.1
  109.  
  110. NoCz    endp                ;                v1.1
  111.  
  112.     even                ;                v1.1
  113. buffer    LABEL    NEAR
  114. BUFSIZ    equ    NOT ($ - CSEG + 256)    ;use full segment        v1.1
  115.                     ;minus 256-byte stack
  116.                     ;and program size
  117.  
  118. ;v1.1    Parse commandline for target filename
  119. ;    Handle redirection spaces
  120.  
  121. Parse_CmdLine    proc    near
  122.  
  123.     mov    si,offset cmdline    ;cmd parm length byte
  124.     lodsb                ;snarf length byte
  125.                     ;(SI -> 1st char)
  126.     xor    ah,ah            ;clear msb
  127.     mov    cx,ax            ;cmdline length is loop counter
  128.     jcxz    Help            ;no cmdline, give help and die
  129.  
  130. ;v1.1    When redirection happens, DOS leaves spaces instead of simply
  131. ;    a null line!
  132. ;    Gotta allow for that by gobbling leading spaces.
  133. ;    If spaces is all we had .. we have redirection!
  134.  
  135.     xor    dx,dx            ;clear DX (don't have a name)
  136.     mov    ah,20H            ;handy constant
  137.  
  138. ParseLup:
  139.     lodsb                ;snarf cmdline char
  140.     cmp    al,ah            ;space?
  141.     jz    Relup            ;yep, skip it
  142.      cmp    al,CR            ;terminating CR? (no name)
  143.      jnz    Got_Name        ;nope, must be real name
  144. Relup:
  145.     loop    ParseLup        ;do all cmdline chars
  146.  
  147. ;If we fall thru .. there were only spaces (redirection).
  148.  
  149.     ret                ;with DX = 0 (no name)
  150.  
  151. ;SI -> target filename's first char (non-space)
  152.  
  153. Got_Name:
  154.     mov    dx,si            ;DX -> filename's first char +1
  155.     dec    dx            ;back up to real first char
  156.     mov    ax,CR            ;scan for terminating CR
  157.                     ;AH=0
  158. Name_Lup:
  159.     cmp    [si],al            ;terminating CR?
  160.     jz    AsciiZ_Name        ;yep, stuff a 0 there
  161.      inc    si            ;bump filename ptr
  162.      loop    Name_Lup        ;nope, keep going
  163.  
  164. ;If we fell through .. something is SEVERELY wrong with our
  165. ;command line count!  Shouldn't ever happen.
  166.  
  167. AsciiZ_Name:
  168.     mov    [si],ah            ;AsciiZ the name
  169.     ret
  170.     
  171. Parse_CmdLine    endp
  172.  
  173.  
  174. ;v1.1    Provide help if no cmd line parms
  175.  
  176. Help    proc    near            ;                v1.1
  177.     mov    dx,offset help$        ;help msg
  178.     mov    ah,9            ;display msg
  179.     int    21H
  180.     mov    ax,4C00H        ;terminate (errorlevel 0)    v1.1
  181.     int    21H            ;                v1.1
  182.  
  183. help$    db    'NoCZ v1.1 - strips Ctrl Z characters from a file.',CR,LF
  184.     db    'Usage:  nocz [<] filename.typ [>output]',CR,LF
  185.     db    'Input may be a proper path:\filename or redirection.',CR,LF
  186.     db    'Output may be redirected as desired (default CON:).'
  187.     db    CR,LF,'$'
  188.  
  189. Help    endp
  190.  
  191. CSEG    ENDS
  192.     END    NoCz
  193.